package com.csg.boot.starter.service.impl;

import com.csg.boot.starter.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * 用户服务实现
 *
 * @author george
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    /**
     * 密码加密转换器
     */
    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 根据用户名获取用户对象
     *
     * @param username 用户名
     * @return 用户对象
     */
    @Override
    public UserDetails loadUserByUsername(String username) {
        log.info("登录用户的账号：" + username);
        String password = passwordEncoder.encode("123456");
        log.info("登录用户的密码：" + password);
        return new User(username, password,
                true, true, true, true,
                AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
    }

    /**
     * 修改密码
     *
     * @param user        用户信息
     * @param newPassword 新密码
     * @return 用户信息
     */
    @Override
    public UserDetails updatePassword(UserDetails user, String newPassword) {
        log.info("new password is " + newPassword);
        return user;
    }
}
